import axios from 'axios'
import JSONbig from 'json-bigint'
// 默认显示劫持公共加域名
axios.defaults.baseURL = 'http://ttapi.research.itcast.cn'
// axios拦截器
axios.interceptors.request.use(
  config => {
    // console.log(config)
    let token = ''
    const obj = JSON.parse(localStorage.getItem('info'))
    if (obj) {
      token = obj.token
      console.log(token)
    }
    // 统一设置请求头
    config.headers.Authorization = `Bearer ${token}`
    return config
  },
  error => {
    return Promise.error(error)
  }
)
// 3. 补充transformResponse处理大整数问题
axios.defaults.transformResponse = [function (data) {
  try {
    return JSONbig.parse(data)
  } catch (err) {
    console.log(err)
    return data
  }
}]

export default axios
